# VIEW
import kits
import model
import random
from time import sleep as sleep

def snapshot(field):
    snapshot.calls += 1
    array = []
    for string in field:
        line = []
        for item in string:
            if len(item) == 0:
                line += ['.']                
            elif str(type(item[0])) == "<class 'model.Red'>":
                line += ['R']
            elif str(type(item[0])) == "<class 'model.Green'>":
                line += ['G']
            elif str(type(item[0])) == "<class 'model.Blue'>":
                line += ['B']
            else:
                line += ['E']
        array += [line]
    return array

def show_snapshot(pict):
    print('---ITERATION # {0}----'.format(snapshot.calls))
    for string in pict:
        print(*string)
    print()
    print('-' * 30)
    
def main_loop(field, green_delay):
    snapshot.calls = 0
    blue_colonia = model.Blue_Cells([])
    red_colonia = model.Red_Cells([])
    green_colonia = model.Green_Cells([])

    file = open('data.txt', 'w')
    
    green_cells_list = []
    red_cells_list = []
    
    
    for i in range(50):
        green_cells_list += [model.Green(random.randint(1, 29), random.randint(1, 29), 5, green_colonia, field)]
    for i in range(5):
        red_cells_list += [model.Red(random.randint(1, 29), random.randint(1, 29), 0.92, red_colonia, blue_colonia, field)]
    
    green_colonia.cells += green_cells_list
    red_colonia.cells += red_cells_list
    
    for i in range(1000):
        
        '''Blue colonia => blue_colonia (Blue_Cells type)
           Red colonia => red_colonia (Red_Cells type)
           Green colonia => green_colonia (Green_Cells type)
           Field => field
           Length of field => length
           Height of field => height'''
        
        kits.save_situation([red_colonia, green_colonia, blue_colonia], i)
        
        if not kits.is_life([red_colonia, green_colonia, blue_colonia], 0.001):
            print("ALL DEAD. GAME OVER. HA-HA.")
            break
        
        if not kits.is_virus(red_colonia):
            print("COLONIA HAVE SURVIVED. YOU WIN, LUCKER.")
            break 
        
        red_colonia.all_check_infect()
        blue_colonia.all_check_delete()
        
        if i % green_delay == 0:
            green_colonia.all_methos()
        
        


        blue_colonia.all_move()
        red_colonia.all_move()
        green_colonia.all_move()
        model.fill_field([blue_colonia, red_colonia, green_colonia], field)
        picture = snapshot(field)
        #sleep(0.2)
        show_snapshot(picture)
        if i == 999:
            print("THE TIME HAS GONE. IT WASNT CLOSE ENOUGH.")

field = [[[]] * 30 for i in range(30)] 
main_loop(field, 80)